package com.music.common.presentation.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.music.common.entity.Media;
import com.music.common.errorhandler.PresentationException;
import com.music.common.presentation.forms.ImportDirectoryForm;
import com.music.common.service.IMediaService;
import com.music.common.service.impl.MediaService;

public class ViewMediaFromDirectoryAction extends Action {

	@SuppressWarnings("finally")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward actionForward = mapping.findForward("fail");
		try {
			IMediaService mediaService = new MediaService();
			ImportDirectoryForm importForm = (ImportDirectoryForm) form;
			String directory = importForm.getDirectory();
			List<Media> medias = mediaService.getMediaListByDirectory(directory, request);
			//List<Media> list = (List<Media>) medias;
			//Media med = mediaService.getMediaById(1);
			//String jsonResponse = JavaToJSONUtil.getJsonString(media);
			//request.setAttribute("response", jsonResponse);
			request.setAttribute("media", medias);
			
			actionForward = (mapping.findForward("success"));
		} catch (Exception ex) {
			throw new PresentationException(ex);
			//System.out.println("Failed at server : \n " + ex.getLocalizedMessage());
		} finally{
			return actionForward;
		}
	}
}
